<template>
	<view class="container">
		<view class="yy-add-Title">
			
			<!-- 新增就诊人 -->
			<text v-if="status == 0">新增就诊人</text>
			
			
			<!-- 编辑就诊人 -->
			<text v-else>编辑就诊人</text>
		</view>
		
		
		
		
		
		<view class="yy-add-con">
			<view class="yy-add-con-item">
				<text class="">姓名</text>
				<view class="yy-add-con-itemIpt">
					<input type="text" value="" v-model="name" placeholder-style="color:#BCC1CC" placeholder="请输入姓名" />
				</view>
			</view>
			<view class="yy-add-con-item">
				<text class="">身份证号</text>
				<view class="yy-add-con-itemIpt">
					<input type="text" value="" v-model="Id_card" @blur="checkage" placeholder-style="color:#BCC1CC" placeholder="请输入身份证号" />
				</view>
			</view>
			<view class="yy-add-con-item">
				<text class="">年龄</text>
				<view class="yy-add-con-itemIpt">
					<input type="text" :disabled="disabled" v-model="age" placeholder-style="color:#BCC1CC" />
				</view>
			</view>
			<view class="yy-add-con-item yy-add-con-Sitem">
				<text class="">手机号</text>
				<view class="yy-add-con-itemIpt">
					<input type="text" value="" v-model="tel" placeholder-style="color:#BCC1CC" placeholder="请输入手机号" />
				</view>
			</view>
			<!-- <view class="yy-add-con-item">
				<text class="">证件类型</text>
				<view class="yy-add-con-itemIpt yy-add-con-SitemIpt">
					<input type="text" value="" disabled="true" placeholder-style="color:#BCC1CC" placeholder="请选择证件类型" />
					<view class="yy-add-con-SitemIptImg">
						<image src="../../static/icon_right.png"></image>
					</view>
				</view>
			</view> -->
		</view>
		
		
		
		
		
		<!-- 新增就诊人保存 -->
		<view class="yy-add-footer" v-if="status == 0" @click="preservation">保存</view>
		
		
		
		<!-- 编辑就诊人保存 -->
		<view class="yy-add-edit" v-else>
			<view class="yy-add-editDel" @click="deluser">删除</view>
			<view class="yy-add-editadd" @click="edituser">保存</view>
		</view>
	</view>
</template>

<script>
	import app from '../../App.vue'
	export default {
		data() {
			return {
				status:'',
				disabled:true,
				name:'',
				age:'',
				tel:'',
				Id_card:'',
				id:'',			//修改用户id
			}
		},
		onLoad(options) {
			this.status = options.status
			if(options.id){
				this.id = options.id
			}
		},
		onShow() {
			if(this.id != ''){
				this.edituserInfo()
			}
		},
		methods: {
			
			analyzeIDCard(IDCard){				//计算年龄
				var sexAndAge = ''
				//获取用户身份证号码
				var userCard = IDCard;
				//如果身份证号码为undefind则返回空
				if(!userCard){
					return sexAndAge;
				}
				//获取性别
				// if(parseInt(userCard.substr(16,1)) % 2 == 1){
				// 	sexAndAge.sex = '1（男）'
				// }else{
				// 	sexAndAge.sex = '0（女）'
				// }
				//获取出生年月日
				//userCard.substring(6,10) + "-" + userCard.substring(10,12) + "-" + userCard.substring(12,14);
				var yearBirth = userCard.substring(6,10);
				var monthBirth = userCard.substring(10,12);
				var dayBirth = userCard.substring(12,14);
				//获取当前年月日并计算年龄
				var myDate = new Date();
				var monthNow = myDate.getMonth() + 1;
				var dayNow = myDate.getDay();
				var age = myDate.getFullYear() - yearBirth;
				if(monthNow < monthBirth || (monthNow == monthBirth && dayNow < dayBirth)){
					age--;
				}
				//得到年龄
				sexAndAge = age;
				//返回性别和年龄
				return sexAndAge;
			},
			checkIDCard(idcode){				//校验身份证
			    // 加权因子
			    var weight_factor = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2];
			    // 校验码
			    var check_code = ['1', '0', 'X' , '9', '8', '7', '6', '5', '4', '3', '2'];
			
			    var code = idcode + "";
			    var last = idcode[17];//最后一位
			
			    var seventeen = code.substring(0,17);
			
			    // ISO 7064:1983.MOD 11-2
			    // 判断最后一位校验码是否正确
			    var arr = seventeen.split("");
			    var len = arr.length;
			    var num = 0;
			    for(var i = 0; i < len; i++){
			        num = num + arr[i] * weight_factor[i];
			    }
			    
			    // 获取余数
			    var resisue = num%11;
			    var last_no = check_code[resisue];
			
			    // 格式的正则
			    // 正则思路
			    /*
			    第一位不可能是0
			    第二位到第六位可以是0-9
			    第七位到第十位是年份，所以七八位为19或者20
			    十一位和十二位是月份，这两位是01-12之间的数值
			    十三位和十四位是日期，是从01-31之间的数值
			    十五，十六，十七都是数字0-9
			    十八位可能是数字0-9，也可能是X
			    */
			    var idcard_patter = /^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$/;
			
			    // 判断格式是否正确
			    var format = idcard_patter.test(idcode);
			
			    // 返回验证结果，校验码和格式同时正确才算是合法的身份证号码
			    return last === last_no && format ? true : false;
			},
			checkage(){				//校验身份证和计算年龄
				// this.analyzeIDCard('410823199909210399')
				let that = this
				let ID_card = this.checkIDCard(that.Id_card)
				console.log(ID_card)
				if(ID_card){
		 			that.age = this.analyzeIDCard(that.Id_card)
				}else{
					that.age = ''
					uni.showToast({
						title:'请输入正确的身份证',
						icon:'none',
						mark:true
					})
				}
			},
			preservation(){				//新增就诊人
				let that = this,
				data = {
					name:that.name,
					mobile:that.tel,
					id_no:that.Id_card
				}
				app.post('api/user_member/addUserMember',data).then(res=>{
					if(res.code == 1){
						console.log(res)
						uni.showLoading({
							title:'加载中',
							success() {
								setTimeout(function(){
									uni.hideLoading()
									uni.navigateTo({
										url:'/pages/person/manage'
									})
								},1300)
							}
						})
					}else if(res.code == 0){
						uni.showToast({
							title:res.msg,
							icon:'none',
							mark:true
						})
					}else if(res.code == 401){
						uni.showToast({
							title:res.msg,
							icon:'none',
							mark:true
						})
					}
				}).catch(err=>{
					uni.showToast({
						title:res.msg,
						icon:'none',
						mark:true
					})
				})
			},
			edituserInfo(){					//就诊人信息回显
				let that = this,
				data = {
					user_member_id:that.id
				}
				app.post('api/user_member/getUserMemberInfo',data).then(res=>{
					if(res.code == 1){
						that.name = res.data.name
						that.age = res.data.age
						that.tel = res.data.mobile
						that.Id_card = res.data.id_no
					}else if(res.code == 0){
						uni.showToast({
							title:res.msg,
							icon:'none',
							mark:true
						})
					}else if(res.code == 401){
						uni.showToast({
							title:res.msg,
							icon:'none',
							mark:true
						})
					}
				}).catch(err=>{
					uni.showToast({
						title:res.msg,
						icon:'none',
						mark:true
					})
				})
			},
			deluser(){					//删除就诊人
				let that = this,
				data = {
					user_member_id:that.id
				}
				app.post('api/user_member/delUserMember',data).then(res=>{
					if(res.code == 1){
						uni.showLoading({
							title:'正在删除',
							success() {
								setTimeout(function(){
									uni.hideLoading()
									uni.navigateTo({
										url:'/pages/person/manage'
									})
								},1300)
							}
						})
					}else if(res.code == 0){
						uni.showToast({
							title:res.msg,
							icon:'none',
							mark:true
						})
					}else if(res.code == 401){
						uni.showToast({
							title:res.msg,
							icon:'none',
							mark:true
						})
					}
				}).catch(err=>{
					uni.showToast({
						title:res.msg,
						icon:'none',
						mark:true
					})
				})
			},
			edituser(){					//编辑就诊人
				let that = this,
				data = {
					user_member_id:that.id,
					name:that.name,
					mobile:that.tel,
					id_no:that.Id_card
				}
				app.post('api/user_member/editUserMember',data).then(res=>{
					if(res.code == 1){
						uni.showLoading({
							title:'正在保存',
							success() {
								setTimeout(function(){
									uni.hideLoading()
									uni.navigateTo({
										url:'/pages/person/manage'
									})
								},1300)
							}
						})
					}else if(res.code == 0){
						uni.showToast({
							title:res.msg,
							icon:'none',
							mark:true
						})
					}else if(res.code == 401){
						uni.showToast({
							title:res.msg,
							icon:'none',
							mark:true
						})
					}
				}).catch(err=>{
					uni.showToast({
						title:res.msg,
						icon:'none',
						mark:true
					})
				})
			}
		}
	}
</script>

<style>
	@import url("../../common/yuyueAddPatient.css");
</style>
